<?php

//
// Advanced checking for variable emptyness
//
function fn_is_empty($var)
{
    if (!is_array($var)) {
		return (empty($var));
    } else {
        foreach ($var as $k => $v) {
			if (empty($v)) {
				unset($var[$k]);
				continue;
			}

			if (is_array($v) && fn_is_empty($v)) {
				unset($var[$k]);
            }
        }
        return (empty($var)) ? true : false;
    }
}

function fn_is_not_empty($var)
{
	return !fn_is_empty($var);
}

function fn_load_module($URL)
{
	 $aryURL = explode("/", $URL['path']);
	 $filen = end($aryURL) ;
	 $filem = substr( $filen, 0, ( strrpos($filen, '.') ) ) . '.php';
	 $filem = ($filen)? $filem:'index.php';
	 if( sizeof($aryURL) > 2 )
	 	$file = DIR_MODULE . $aryURL[1] . "/" . $filem;
	 else
	 	$file = DIR_MODULE . $filem;
	 return $file;
}

function fn_load_admin_module($URL)
{
	 $filen = end(explode("/", $URL['path'])) ;
	 $filem = substr( $filen, 0, ( strrpos($filen, '.') ) ) . '.php';
	 $file = DIR_MODULE . $filem;
	 return $file;
}

function fn_clear_auth_session()
{

		$_SESSION['AUTH'] = '';
		unset($_SESSION['AUTH']);
    session_unset();
    session_destroy();
}

function fn_alertMsg($msg, $url='')
{
		if( empty($url) )
		{
			$url = 'javascript:history.go(-1)';
		}else
			$url = 'window.location.href="'.$url.'"';
		echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
		echo '<script language="JAVASCRIPT">
					alert("'.$msg.'");
					'.$url.';</script>';
		exit;
}

//
// The function returns Host IP and Proxy IP.
//
function fn_get_ip($return_int = false)
{
	  if (!empty($_SERVER['HTTP_CLIENT_IP']))
	  //check ip from share internet
	  {
	    $ip=$_SERVER['HTTP_CLIENT_IP'];
	  }
	  elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
	  //to check ip is pass from proxy
	  {
	    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
	  }
	  else
	  {
	    $ip=$_SERVER['REMOTE_ADDR'];
	  }
	  return $ip;
}
//
// If there is IP address in address scope global then return true.
//
function fn_is_inet_ip($ip, $is_int = false)
{
	if ($is_int) {
		$ip = long2ip($ip);
	}
	$_ip = explode('.', $ip);
	return
		($_ip[0] == 10 ||
		($_ip[0] == 172 && $_ip[1] >= 16 && $_ip[1] <= 31) ||
		($_ip[0] == 192 && $_ip[1] == 168) ||
		($_ip[0] == 127 && $_ip[1] == 0 && $_ip[2] == 0 && $_ip[3] == 1) ||
		($_ip[0] == 255 && $_ip[1] == 255 && $_ip[2] == 255 && $_ip[3] == 255))
		? false : true;
}



?>